//
//  TabBarView.m
//  dyss_v2
//
//  Created by  smarthome on 16/1/27.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "DYTabBarView.h"
#import "DYTabBarButton.h"


@interface DYTabBarView ()

@property (nonatomic, weak) DYTabBarButton *currButton;

@end

@implementation DYTabBarView

- (void)addTabbarButton:(UITabBarItem *)item {
    // 1,创建 自定义的 Button
    DYTabBarButton * button=[DYTabBarButton buttonWithType:UIButtonTypeCustom];
    
    // 2,设置图片 normal / selected
    [button setBackgroundImage:item.image forState:UIControlStateNormal];
    [button setBackgroundImage:item.selectedImage forState:UIControlStateSelected];
    
    // 3,设置监听
    [button addTarget:self action:@selector(button_onClick:) forControlEvents:UIControlEventTouchDown];
    
    // 4,添加到
    [self addSubview:button];
    
    if(self.subviews.count==1)
    {
        [self button_onClick:button];
    }
}

-(void)layoutContentViews{
    DYTabBarButton *btn1 = self.subviews[0];
    DYTabBarButton *btn2 = self.subviews[1];
    DYTabBarButton *btn3 = self.subviews[2];
    DYTabBarButton *btn4 = self.subviews[3];
    
    btn1.tag = 0;
    btn2.tag = 1;
    btn3.tag = 2;
    btn4.tag = 3;
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(btn2);
        make.height.equalTo(self.mas_height);
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top).offset(0.5);
    }];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(btn3);
        make.height.equalTo(self.mas_height);
        make.left.equalTo(btn1.mas_right);
        make.top.equalTo(self.mas_top).offset(0.5);
    }];
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(btn4);
        make.height.equalTo(self.mas_height);
        make.left.equalTo(btn2.mas_right);
        make.top.equalTo(self.mas_top).offset(0.5);
    }];
    [btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(btn1);
        make.height.equalTo(self.mas_height);
        make.left.equalTo(btn3.mas_right);
        make.top.equalTo(self.mas_top).offset(0.5);
        make.right.equalTo(self.mas_right);
    }];
}

//- (void)layoutSubviews {
//    // 摁钮的数量
//    int count=(int)self.subviews.count;
//    
//    // 1,btn,
//    CGFloat btnW=self.frame.size.width/count;
//    CGFloat btnH=self.frame.size.height;
//    CGFloat btnY=0;
//    // 2,遍历
//    for (int index=0; index<count; index++) {
//        DYTabBarButton * btn=self.subviews[index];
//        CGFloat btnX=btnW*index;
//        btn.frame=CGRectMake(btnX, btnY, btnW, btnH);
//        btn.tag=index;
//    }
//}

- (void)button_onClick:(DYTabBarButton *)button {
    if (button == self.currButton) {
        printD(@"已经是当前页面了...");
        return;
    }
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectItemFrom:to:)]) {
        [self.delegate tabBar:self didSelectItemFrom:self.currButton.tag to:button.tag];
    }
    self.currButton.selected = NO;
    button.selected=YES;
    self.currButton=button;
}

-(void)switchToIndex:(NSInteger)index{
    DYTabBarButton *btn = self.subviews[index];
    [self button_onClick:btn];
}

@end
